package cn.jgy.dynamic.thread.pool.sdk.trigger.job;


import cn.jgy.dynamic.thread.pool.sdk.domain.IDynamicThreadPoolService;
import cn.jgy.dynamic.thread.pool.sdk.domain.model.entity.ThreadPoolConfigEntity;
import cn.jgy.dynamic.thread.pool.sdk.registry.IRegistry;
import com.alibaba.fastjson.JSON;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.scheduling.annotation.Scheduled;

import java.util.List;


/**
* @Author: 蒋光耀
*@Version: 1.0
*@description: 线程池数据上报任务
* @Date:  2024-05-18 13:13
*/
public class ThreadPoolDataReportJob {

    private final Logger logger = LoggerFactory.getLogger(ThreadPoolDataReportJob.class);

    //通过服务拿数据
    private final IDynamicThreadPoolService dynamicThreadPoolService;
    //通过redis进行上报
    private final IRegistry registry;

    //使用构造函数传入值
    public ThreadPoolDataReportJob(IDynamicThreadPoolService dynamicThreadPoolService, IRegistry registry) {
        this.dynamicThreadPoolService = dynamicThreadPoolService;
        this.registry = registry;
    }

    @Scheduled(cron = "0/20 * * * * ?")
    public void execReportThreadPoolList() {
        List<ThreadPoolConfigEntity> threadPoolConfigEntities = dynamicThreadPoolService.threadPoolList();
        registry.reportThreadPoolList(threadPoolConfigEntities);
        logger.info("动态线程池，上报线程池信息：{}", JSON.toJSONString(threadPoolConfigEntities));

        for (ThreadPoolConfigEntity threadPoolConfigEntity : threadPoolConfigEntities) {
            registry.reportThreadPoolOne(threadPoolConfigEntity);
            logger.info("动态线程池，上报线程池配置：{}", JSON.toJSONString(threadPoolConfigEntity));
        }

    }

}
